# 这是一个父子字段转化为tree的函数
import json

from models.entity.district_item import DistrictItem


def get_children(id_num=-1, source=0):
    """给定一个节点，返回他的孩子节点"""
    sz = []
    for obj in source:
        if obj.parent_id == id_num:
            sz.append({"id": obj.id, "name": obj.name, "url": obj.url, "target": obj.target, "icon": obj.icon,
                       "children": get_children(obj.id)})
    return sz


# print(json.dumps(get_children()))


# 这是给dict1_cateory准备的函数
def get_children_dict_category(id_num=-1, source=0):
    """给定一个节点，返回他的孩子节点"""
    sz = []
    for obj in source:
        if obj.parent_id == id_num:
            sz.append({"id": obj.id, "name": obj.name, "code": obj.code, "description": obj.description,
                       "dis_order": obj.dis_order,
                       "status": obj.status, "children": get_children(obj.id)})
    return sz


# model 专用  勿忘
# def get_all_model(source, id_num=-1):
#     """给定一个节点，返回他的孩子节点"""
#     sz = []
#     for obj in source:
#         if obj.parent_id == id_num:
#             sz.append({"id": obj.id, "name": obj.name, "code": obj.code, "description": obj.description,
#                        "dis_order": obj.dis_order,
#                        "status": obj.status, "target": obj.target, "url": obj.url, "is_public": obj.is_public,
#                        "is_expand": obj.is_expand, "children": get_all_model(obj.id)})
#     return sz


def get_all_model(source, id_num):
    # id_num为最顶层元素的parent_id的值
    rlist = []

    def get_all_model_in(in_id_num):
        sz = []
        for obj in source:
            # if obj.parent_id == in_id_num:
            if obj.ParentId == in_id_num:
                sz.append({"id": obj.id, "name": obj.name, "code": obj.code, "description": obj.description,
                           "dis_order": obj.dis_order, "icon": obj.icon,
                           "status": obj.status, "url": obj.url, "is_public": obj.is_public, "ParentId": obj.ParentId,
                           "target": obj.target,
                           "is_expand": obj.is_expand, "children": get_all_model_in(obj.id)})
        return sz

    print("-----------------------------------")
    tree_list = get_all_model_in(id_num)
    if tree_list == []:
        return []
    rlist.append(tree_list[0])
    return rlist


def get_all_dict_cate(source, id_num):
    rlist = []

    def get_all_model_in(in_id_num):
        sz = []
        for obj in source:
            if obj.parent_id == in_id_num:
                sz.append({"id": obj.id, "name": obj.name, "code": obj.code, "description": obj.description,
                           "dis_order": obj.dis_order, "status": obj.status, "children": get_all_model_in(obj.id)})
        return sz

    print("-----------------------------------")
    rlist.append(get_all_model_in(id_num)[0])
    return rlist


def get_all_area(source, id_num=-1):
    """给定一个节点，返回他的孩子节点"""
    sz = []
    for obj in source:
        if obj.parent_id == id_num:
            sz.append({"id": obj.id, "name": obj.name, "short_name": obj.short_name, "longitude": obj.longitude,
                       "latitude": obj.latitude,
                       "sort": obj.sort, "status": obj.status})
    return sz


# Button专用
def get_all_button(id_num=-1, source=0):
    """给定一个节点，返回他的孩子节点"""
    sz = []
    for obj in source:
        if obj.parent_id == id_num:
            sz.append({"id": obj.id, "name": obj.name, "code": obj.code, "": obj.target,
                       "url": obj.url,
                       "sort": obj.sort, "status": obj.status})
    return sz


# 得到所有的设备树
def get_all_device(source, id_num):
    rlist = []

    def get_all_model_in(in_id_num):
        sz = []
        for obj in source:
            if obj.parent_id == in_id_num:
                tdic = obj.get_dic()
                tdic["children"] = get_all_model_in(obj.id)
                sz.append(tdic)
        return sz

    rlist.append(get_all_model_in(id_num)[0])
    return rlist


def get_all_button(source, id_num):
    rlist = []

    def get_all_model_in(in_id_num):
        sz = []
        for obj in source:
            if obj.parent_id == in_id_num:
                sz.append({"id": obj.id, "name": obj.name, "code": obj.code, "module_id": obj.module_id,
                           "parent_id": obj.parent_id,
                           "dis_order": obj.dis_order, "url": obj.url, "status": obj.status,
                           "target": obj.target, "children": get_all_model_in(obj.id)})
        return sz

    node_tree_list = get_all_model_in(id_num)
    if node_tree_list != []:
        rlist.append(get_all_model_in(id_num)[0])
    else:
        rlist = []
    return rlist


def get_district_tree(source, id_num):   #  [6个], [1个]分区的列表 ,  0
    print('第一步')
    rlist = []
    def get_all_model_in(in_id_num):  # 0 , 1
        sz = []
        for obj in source:      # 分区测试1 ,111
            print('obj', obj)
            print('obj.pid:', obj.parent_id, 'in_id_num:', in_id_num)
            if obj.parent_id == in_id_num:   # 0=0  , 0!=1 , 1
                print('obj.pid:',obj.parent_id, 'in_id_num:', in_id_num)
                item = []
                for tt in DistrictItem.get_district_items_by_district(obj.id):
                    item.append(tt.get_dic())
                sz.append({"id": obj.id, "name": obj.name, "code": obj.code, "description": obj.description,
                           "dis_order": obj.dis_order, "status": obj.status, "ParentId": obj.parent_id,
                           "lat": obj.lat, "lng": obj.lng, "item": item, "children": get_all_model_in(obj.id)}) # 1
                print('parent_id == in_id_num')
            print('第二次返回')
        print('sz',len(sz), sz)
        return sz
    print('@@@@@@@@@')

    if len(get_all_model_in(id_num)[0]):
        print('第二部?')
        print(555555555,get_all_model_in(id_num)[0])
        print('调用内部函数in')
        rlist.append(get_all_model_in(id_num)[0])
    else:
        print('else')
        rlist = []
    return rlist


def get_all_quarter(source, id_num):
    rlist = []

    def get_all_quarter_in(in_id_num):
        sz = []
        for obj in source:
            # if obj.p_quarter == in_id_num:  # 原来的方法
            if obj.parent_id == in_id_num:    # 我的方法
                sz.append({"id": obj.id, "name": obj.name, "code": obj.code, "department_id": obj.department_id,
                           "dis_order": obj.dis_order, "create_time": obj.create_time,
                           "parent_id": obj.parent_id, "children": get_all_quarter_in(obj.id)})
        return sz

    if len(get_all_quarter_in(id_num)[0]):
        print(get_all_quarter_in(id_num)[0])
        rlist.append(get_all_quarter_in(id_num)[0])
    else:
        rlist = []
    return rlist
